package com.zyj.spring.annotationaop.dao;

import com.zyj.spring.annotationaop.annatation.NeedSetValueField;
import com.zyj.spring.annotationaop.model.Order;
import com.zyj.spring.annotationaop.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class OrderDao {

    private List<Order> fakeOrders;

    @Autowired
    private UserDao userDao;

    @PostConstruct
    private void init() {
        fakeOrders = new ArrayList<>(2 << 2);
        fakeOrders.add(Order.builder().id(1L).customerId(1L).build());
        fakeOrders.add(Order.builder().id(2L).customerId(2L).build());
        fakeOrders.add(Order.builder().id(3L).customerId(3L).build());
        fakeOrders.add(Order.builder().id(4L).customerId(4L).build());
        fakeOrders.add(Order.builder().id(5L).customerId(5L).build());
    }

    @NeedSetValueField
    public List<Order> selectList() {

        // 为每个Order的customerName赋值
//        fakeOrders.forEach(order -> {
//            User user = userDao.selectOne(order.getCustomerId());
//            if (Objects.nonNull(user)) {
//                order.setCustomerName(user.getName());
//            }
//        });

        return fakeOrders;
    }

}
